how to deploy nacos on kubernetes1.19

  1. deploy nfs

    ansible -i hosts nodes -m shell -a "yum install nfs-utils -y"
    git clone https://github.com/nacos-group/nacos-k8s.git
    cd nacos-k8s/deploy/nfs
    
    cat > deployment.yaml << EOF
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: nfs-client-provisioner
    ---
    kind: Deployment
    apiVersion: apps/v1
    metadata:
      name: nfs-client-provisioner
    spec:
      replicas: 1
      strategy:
        type: Recreate
      selector:
        matchLabels:
          app: nfs-client-provisioner
      template:
        metadata:
          labels:
            app: nfs-client-provisioner
        spec:
          serviceAccount: nfs-client-provisioner
          containers:
            - name: nfs-client-provisioner
              image: quay.io/external_storage/nfs-client-provisioner:latest
              volumeMounts:
                - name: nfs-client-root
                  mountPath: /persistentvolumes
              env:
                - name: PROVISIONER_NAME
                  value: fuseim.pri/ifs
                - name: NFS_SERVER
                  value: 192.168.0.209
                - name: NFS_PATH
                  value: /nfs/kubernetes
          volumes:
            - name: nfs-client-root
              nfs:
                server: 192.168.0.209
                path: /nfs/kubernetes
    EOF
    
    cat class.yaml
    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: managed-nfs-storage
    provisioner: fuseim.pri/ifs
    parameters:
      archiveOnDelete: "false"
    
    cat rbac.yaml
    kind: ClusterRole
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      name: nfs-client-provisioner-runner
    rules:
    - apiGroups: [""]
      resources: ["persistentvolumes"]
      verbs: ["get", "list", "watch", "create", "delete"]
    - apiGroups: [""]
      resources: ["persistentvolumeclaims"]
      verbs: ["get", "list", "watch", "update"]
    - apiGroups: [""]
      resources: ["endpoints"]
      verbs: ["get", "list", "watch", "create", "update", "patch"]
    - apiGroups: ["storage.k8s.io"]
      resources: ["storageclasses"]
      verbs: ["get", "list", "watch"]
    - apiGroups: [""]
      resources: ["events"]
      verbs: ["create", "update", "patch"]
    ---
    kind: ClusterRoleBinding
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      name: run-nfs-client-provisioner
    subjects:
    - kind: ServiceAccount
      name: nfs-client-provisioner
      namespace: default
    roleRef:
      kind: ClusterRole
      name: nfs-client-provisioner-runner
      apiGroup: rbac.authorization.k8s.io
    ---
    kind: Role
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      name: leader-locking-nfs-client-provisioner
    rules:
    - apiGroups: [""]
      resources: ["endpoints"]
      verbs: ["get", "list", "watch", "create", "update", "patch"]
    ---
    kind: RoleBinding
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      name: leader-locking-nfs-client-provisioner
    subjects:
    - kind: ServiceAccount
      name: nfs-client-provisioner
      # replace with namespace where provisioner is deployed
      namespace: default
    roleRef:
      kind: Role
      name: leader-locking-nfs-client-provisioner
      apiGroup: rbac.authorization.k8s.io
    
    kubectl create -f deploy/nfs/rbac.yaml
    kubectl create -f deploy/nfs/class.yaml
    kubectl apply -f deploy/nfs/deployment.yaml
    kubectl patch storageclass managed-nfs-storage -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
    
  2. deploy mysql

      cat > mysql/mysql-nfs.yaml << EOF
      apiVersion: v1
      kind: ReplicationController
      metadata:
        name: mysql
        labels:
          name: mysql
      spec:
        replicas: 1
        selector:
          name: mysql
        template:
          metadata:
            labels:
              name: mysql
          spec:
            containers:
            - name: mysql
              image: nacos/nacos-mysql:5.7
              ports:
              - containerPort: 3306
              volumeMounts:
              - name: mysql-data
                mountPath: /var/lib/mysql
              env:
              - name: MYSQL_ROOT_PASSWORD
                value: "nacos"
              - name: MYSQL_DATABASE
                value: "nacos"
              - name: MYSQL_USER
                value: "nacos"
              - name: MYSQL_PASSWORD
                value: "nacos"
            volumes:
            - name: mysql-data
              nfs:
                server: 192.168.0.209
                path: /nfs/mysql
      ---
      apiVersion: v1
      kind: Service
      metadata:
        name: mysql
        labels:
          name: mysql
      spec:
        ports:
        - port: 3306
          targetPort: 3306
        selector:
          name: mysql
    EOF
    
    kubectl apply -f mysql/mysql-nfs.yaml
    
  3. deploy nacos

      cat > nacos/nacos-pvc-nfs.yaml << EOF
      ---
      apiVersion: v1
      kind: Service
      metadata:
        name: nacos
        labels:
          app: nacos
        annotations:
          service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
      spec:
        ports:
          - port: 8848
            name: server
            targetPort: 8848
        clusterIP: None
        selector:
          app: nacos
      ---
      apiVersion: v1
      kind: ConfigMap
      metadata:
        name: nacos-cm
      data:
        mysql.db.name: "nacos"
        mysql.port: "3306"
        mysql.user: "nacos"
        mysql.password: "nacos"
      ---
      apiVersion: apps/v1
      kind: StatefulSet
      metadata:
        name: nacos
      spec:
        serviceName: nacos
        replicas: 3
        template:
          metadata:
            labels:
              app: nacos
            annotations:
              pod.alpha.kubernetes.io/initialized: "true"
          spec:
            affinity:
              podAntiAffinity:
                requiredDuringSchedulingIgnoredDuringExecution:
                  - labelSelector:
                      matchExpressions:
                        - key: "app"
                          operator: In
                          values:
                            - nacos
                    topologyKey: "kubernetes.io/hostname"
            serviceAccountName: nfs-client-provisioner
            initContainers:
              - name: peer-finder-plugin-install
                image: nacos/nacos-peer-finder-plugin:1.0
                imagePullPolicy: Always
                volumeMounts:
                  - mountPath: "/home/nacos/plugins/peer-finder"
                    name: plugindir
            containers:
              - name: nacos
                imagePullPolicy: Always
                image: nacos/nacos-server:latest
                resources:
                  requests:
                    memory: "2Gi"
                    cpu: "500m"
                ports:
                  - containerPort: 8848
                    name: client-port
                env:
                  - name: NACOS_REPLICAS
                    value: "3"
                  - name: SERVICE_NAME
                    value: "nacos"
                  - name: DOMAIN_NAME
                    value: "cluster.local"
                  - name: POD_NAMESPACE
                    valueFrom:
                      fieldRef:
                        apiVersion: v1
                        fieldPath: metadata.namespace
                  - name: MYSQL_SERVICE_DB_NAME
                    valueFrom:
                      configMapKeyRef:
                        name: nacos-cm
                        key: mysql.db.name
                  - name: MYSQL_SERVICE_PORT
                    valueFrom:
                      configMapKeyRef:
                        name: nacos-cm
                        key: mysql.port
                  - name: MYSQL_SERVICE_USER
                    valueFrom:
                      configMapKeyRef:
                        name: nacos-cm
                        key: mysql.user
                  - name: MYSQL_SERVICE_PASSWORD
                    valueFrom:
                      configMapKeyRef:
                        name: nacos-cm
                        key: mysql.password
                  - name: NACOS_SERVER_PORT
                    value: "8848"
                  - name: NACOS_APPLICATION_PORT
                    value: "8848"
                  - name: PREFER_HOST_MODE
                    value: "hostname"
                volumeMounts:
                  - name: plugindir
                    mountPath: /home/nacos/plugins/peer-finder
                  - name: datadir
                    mountPath: /home/nacos/data
                  - name: logdir
                    mountPath: /home/nacos/logs
        volumeClaimTemplates:
          - metadata:
              name: plugindir
              annotations:
                volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
            spec:
              accessModes: [ "ReadWriteMany" ]
              resources:
                requests:
                  storage: 5Gi
          - metadata:
              name: datadir
              annotations:
                volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
            spec:
              accessModes: [ "ReadWriteMany" ]
              resources:
                requests:
                  storage: 5Gi
          - metadata:
              name: logdir
              annotations:
                volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
            spec:
              accessModes: [ "ReadWriteMany" ]
              resources:
                requests:
                  storage: 5Gi
        selector:
          matchLabels:
            app: nacos
    EOF
    
    kubectl apply -f nacos/nacos-pvc-nfs.yaml
    
  4. references:

    https://github.com/nacos-group/nacos-k8s

    https://nacos.io/zh-cn/docs/use-nacos-with-kubernetes.html